/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditAssignedInstrumentView.java
 * Date: 07/02/2011
 */

package com.ingenius.sau.client.view;

import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.presenter.EditAssignedInstrumentPresenter;
import com.ingenius.sau.client.presenter.EditAssignedInstrumentPresenter.EditAssignedInstrumentDisplay;
import com.ingenius.sau.client.view.widgets.ListBox;
import com.ingenius.sau.shared.model.instruments.Instrument;
import com.ingenius.sau.shared.model.users.Group;

/**
 * Class <strong>EditAssignedInstrumentView</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditAssignedInstrumentView extends AbstractEditView implements
		EditAssignedInstrumentDisplay {

	// UI Widgets
	private final ListBox comboInstrumentTypes;
	private final ListBox comboInstruments;
	private final ListBox comboGroups;
	private final TextField<String> textScore;

	// Constructor

	public EditAssignedInstrumentView() {
		super();
		comboInstrumentTypes = new ListBox();
		comboInstruments = new ListBox();
		comboGroups = new ListBox();
		textScore = new TextField<String>();

		// TODO i18n
		setHeading("Edit Assigned Instrument");
		setSize(500, 270);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.views.AbstractEditView#createComponents()
	 */
	@Override
	protected void createComponents() {
		String scoreMessage = AppController
				.getAttribute(Constants.COOKIE_SCALE).toString();
		// TODO i18n
		FormData formData = new FormData("-20");
		comboInstrumentTypes.setFieldLabel("Instrument type");
		comboInstrumentTypes.setEditable(false);
		comboInstrumentTypes.setAllowBlank(false);
		comboInstrumentTypes.addPlugin(plugin);
		comboInstrumentTypes.setData("text", "Select the instrument type");
		comboInstrumentTypes.getMessages().setBlankText("Type is required");

		comboInstruments.setFieldLabel("Instrument");
		comboInstruments.setEditable(false);
		comboInstruments.setAllowBlank(false);
		comboInstruments.addPlugin(plugin);
		comboInstruments.setData("text", "Select the instrument to assign");
		comboInstruments.getMessages().setBlankText("Instrument is required");

		comboGroups.setFieldLabel("Group");
		comboGroups.setEditable(false);
		comboGroups.addPlugin(plugin);
		comboGroups.setData("text", "Select the group");
		comboGroups.getMessages().setBlankText("Group is required");

		textScore.setFieldLabel("Score");
		textScore.setAllowBlank(false);
		textScore.addPlugin(plugin);
		textScore.setData("text", scoreMessage);
		textScore.getMessages().setBlankText("Score is required");

		form.add(comboInstrumentTypes, formData);
		form.add(comboInstruments, formData);
		form.add(comboGroups, formData);
		form.add(textScore, formData);

		registerHandlers();
		enableComboGroups(false);
	}

	private void registerHandlers() {
		comboInstrumentTypes.addListener(Events.SelectionChange,
				new Listener<BaseEvent>() {
					@Override
					public void handleEvent(BaseEvent be) {
						((EditAssignedInstrumentPresenter) presenter)
								.loadInstruments();
					}
				});

	}

	@Override
	public String getInstrumentType() {
		return comboInstrumentTypes.getSimpleValue();
	}

	@Override
	public void setInstrumentType(String type) {
		comboInstrumentTypes.setSimpleValue(type);
	}

	@Override
	public void setInstrumentsTypes(List<String> types) {
		comboInstrumentTypes.clear();
		for (String type : types) {
			comboInstrumentTypes.add(type);
		}
	}

	@Override
	public String getInstrumentId() {
		return comboInstruments.getSimpleValue();
	}

	@Override
	public void setInstrumentId(String instrumentId) {
		comboInstruments.setSimpleValue(instrumentId);
	}

	@Override
	public void setInstruments(List<Instrument> instruments) {
		comboInstruments.clear();
		for (Instrument instrument : instruments) {
			comboInstruments.add(instrument.getId(), instrument.toString());
		}
		setInstrumentId((String) AppController
				.getAttribute(Constants.COOKIE_INSTRUMENT_ID));
	}

	@Override
	public String getGroupId() {
		return comboGroups.getSimpleValue();
	}

	@Override
	public void setGroupId(String groupId) {
		comboGroups.setSimpleValue(groupId);
	}

	@Override
	public void setGroups(List<Group> groups) {
		comboGroups.clear();
		for (Group group : groups) {
			comboGroups.add(group.getId(), group.toString());
		}
	}

	@Override
	public Double getScore() {
		Double result;
		try {
			result = Double.parseDouble(textScore.getValue());
		} catch (NumberFormatException e) {
			result = 0.0;
		}
		return result;
	}

	@Override
	public void setScore(Double score) {
		textScore.setValue("" + score);
	}

	@Override
	public void enableComboGroups(boolean state) {
		comboGroups.setEnabled(state);
		comboGroups.setSimpleValue("");
	}

}
